<?php

namespace App\Http\Controllers\admin\defaults;


use App\Helpers\commonFuc;
use App\Helpers\Upload;


class UploadController
{

    /**图片上传
     * @return \App\Helpers\Json
     */
    function img(){
        $old_path = \request('old_path');
        if($old_path){
            $url = $old_path;
        }else{
            $url = '/uploads/article2/'.date('Y-m-d').'/';
        }
        $url =Upload::uploadFile($_FILES, $url,true);//上传图片 返回名
        return commonFuc::data_reture_json($url,0,'ok');
    }
    function mp4(){
        if(input('name')){
            $url = input('name');
        }else{
            $url = '/uploads/article1/'.date('Y-m-d').'/';
        }
        $url =Upload::uploadFile($_FILES, $url,false,['mp4','avi','rmvb','wav','wma','wmv','fiv']);//上传图片 返回名
        return commonFuc::data_reture_json($url,0,'ok');
    }
    function file(){
        if(input('name')){
            $url = input('name');
        }else{
            $url = '/uploads/article3/'.date('Y-m-d').'/';
        }

        $url =Upload::uploadFile($_FILES, $url,false,['*']);//上传图片 返回名
        return commonFuc::data_reture_json(['url'=>$url,'name'=>$_FILES['file']['name']],0,'ok');
    }

    /**
     * 大文件上传 断点续传
     */
    function filepage(){

        $uploaddir = 'uploads/source/' . date("Ymd") . '/';
        if (!is_dir($uploaddir)) {
            mkdir($uploaddir, 0777, true);
        }
        //dump(input());
        $status = 1;
        //上传文件要保存的路径
        $fname = sprintf($uploaddir . '%s.%s', md5($_POST['fileName']), $_POST['fileExt']);
        $data = file_get_contents($_FILES['file']['tmp_name']);

        if ($_POST['page'] == 1) {
            file_put_contents($fname, $data);
        } else {
            //其余文件追加到文件末尾
            file_put_contents($fname, $data, FILE_APPEND);
        }
        $res = ['status' => $status];
        //最后一片文件
        if ($_POST['totalPage'] == $_POST['page']) {
            $status = 2;
            $newfilename = str_replace(md5($_POST['fileName']), uniqid(), $fname);
            rename($fname, $newfilename);
            $res = ['status' => $status, 'url' => "/" . $newfilename];
        }
        //返回上传状态
        echo json_encode($res);
        //return commonFuc::data_reture_json($res,200,'');
    }


}
